home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / internet config assistant / internetassistant / htmlparser.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-04  |  2.6 KB  |  128 lines

  1. /*
  2.     File:        HTMLParser.h
  3.  
  4.     Contains:    A simplistic parse of a sub-set of HTML.
  5.                 Used to allow styles in dialogs.
  6.  
  7.     Written by:    Arno Gourdol
  8.  
  9.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  10.  
  11. */
  12.  
  13. #pragma once
  14.  
  15. #ifndef __HTMLPARSER__
  16. #define __HTMLPARSER__
  17.  
  18. #include <Script.h>
  19. #include <TextEdit.h>
  20. #include "CRect.h"
  21.  
  22. class HTMLParser
  23. {
  24. public:
  25.     // constructor
  26.     HTMLParser(Handle text, SInt32 fromIndex = 0, SInt32 toIndex = -1);
  27.     
  28.     // destructor
  29.     ~HTMLParser();
  30.     
  31.     void Parse(void);
  32.  
  33. protected:
  34.     
  35.     // Tag hook functions
  36.     virtual void TagBoldStart(SInt32 tagStart, SInt32 tagEnd);
  37.     virtual void TagBoldEnd(SInt32 tagStart, SInt32 tagEnd);
  38.     virtual void TagItalicStart(SInt32 tagStart, SInt32 tagEnd);
  39.     virtual void TagItalicEnd(SInt32 tagStart, SInt32 tagEnd);
  40.     virtual void EndOfText(SInt32 endOfText);
  41.  
  42.     // Tag utility functions
  43.     Boolean IsTag(SInt32 tagStart, SInt32 tagEnd, ConstStr255Param tag);
  44.     void Tag(SInt32 tagStart, SInt32 tagEnd);
  45.     
  46.     // Parse engine utility functions
  47.     void SkipWhiteSpaces(void);    // Move cursor until non white space
  48.     inline void Abort(void);    // Terminates parsing
  49.     UInt16 GetChar(void);    // Returns current character and advance
  50.     SInt32 FindChar(UInt16 c);    // Find a character, returns its index
  51.                                 // do not change cursor
  52.     inline SInt32 GetCursor(void);    // Returns current index
  53.     inline void SetCursor(SInt32 cursor); // Set current index
  54.     inline SInt32 GetFinalCursor(void);
  55.     
  56.     char* fText;
  57.  
  58. private:
  59.  
  60.     Handle fTextHandle;
  61.     SignedByte fSavedHandleState;
  62.     Boolean fContinue;
  63.  
  64.     SInt32 fCursor;        // Current Index in the string
  65.     SInt32 fFinalCursor;
  66. };
  67.  
  68. void HTMLParser::Abort(void)
  69. {
  70.     fContinue = false;
  71. }
  72.  
  73.  
  74. SInt32 HTMLParser::GetCursor(void)
  75. {
  76.     return fCursor;
  77. }
  78.  
  79.  
  80. void HTMLParser::SetCursor(SInt32 cursor)
  81. {
  82.     fCursor = cursor;
  83.     if (fCursor > fFinalCursor)
  84.         Abort();
  85. }
  86.  
  87.  
  88. SInt32 HTMLParser::GetFinalCursor(void)
  89. {
  90.     return fFinalCursor;
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98. class HTMLToStyledText : public HTMLParser
  99. {
  100. public:
  101.     HTMLToStyledText(CRect frame, Handle text);
  102.     
  103.     TEHandle GetStyledText(void);
  104.     
  105. protected:
  106.     void ApplyTextStyles(SInt32 upToIndex);
  107.  
  108.     virtual void TagBoldStart(SInt32 tagStart, SInt32 tagEnd);
  109.     virtual void TagBoldEnd(SInt32 tagStart, SInt32 tagEnd);
  110.     virtual void TagItalicStart(SInt32 tagStart, SInt32 tagEnd);
  111.     virtual void TagItalicEnd(SInt32 tagStart, SInt32 tagEnd);
  112.     virtual void EndOfText(SInt32 endOfText);
  113.  
  114. private:
  115.     CRect fFrame;
  116.     TEHandle fStyledText;
  117.  
  118.     // The fields below are counters representing the state 
  119.     // of the parsing
  120.     SInt32 fLastTextStyleChange;
  121.     UInt16 fBold;
  122.     UInt16 fItalic;
  123. };
  124.  
  125. void ConvertHTMLToStyledText(Handle text, CRect frame, TEHandle* textEdit);
  126.  
  127. #endif
  128.